import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

var db;
// 获取数据库文件的存储路径
Future<void> create() async {
  var databasesPath = await getDatabasesPath();
  String path = join(databasesPath, 'demo.db');

// 创建数据库表
  db = await openDatabase(path, version: 1,
      onCreate: (Database db, int version) async {
        await db.execute('''
        CREATE TABLE cacheT (
            'Id' INTEGER PRIMARY KEY,
            'userId' TEXT,
            'token' TEXT
            )
          ''');
      });
}

// 插入一条书籍数据
Future<void> insert(Map<String, dynamic> map) async {
  await db.insert('cacheT', map);
}

// 根据ID查找书籍信息
Future<String> getBook(int id) async {
  List<Map> maps = await db.query('cacheT',
      columns: ['userId',
          'token'
      ],
      where: 'Id = ?',
      whereArgs: [id]);
  if (maps.length > 0) {
    return maps[0]['userId'];
  }
  return null;
}


// 更新书籍信息
Future<int> update(Map<String, String> map, int id) async {
  return await db.update('cacheT', map,
      where: 'Id = ?', whereArgs: [id]);
}